package com.tiemei.demo.httpserver;

import java.io.IOException;
import java.io.OutputStream;
import java.net.InetSocketAddress;

import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.HttpHandler;
import com.sun.net.httpserver.HttpServer;

public class Test {

	public static void main(String[] args) throws Exception {
		HttpServer server = HttpServer.create(new InetSocketAddress(8089), 0);
//		server.createContext("/LuaMonitor", new MyHandler());
//		server.createContext("/LuaMonitor", new MyHandler2());
		server.createContext("/LuaMonitor", new MyHandler3());
		server.setExecutor(null); // creates a default executor
		server.start();
	}

	static class MyHandler implements HttpHandler {

		public void handle(HttpExchange t) throws IOException {
			String response = "trident_features_cache~1.0~features_feedback$$trident_features~200~4~taobao_search$$trident_features_lua~1000~0.2~4";
			t.sendResponseHeaders(200, response.length());
			OutputStream os = t.getResponseBody();
			os.write(response.getBytes());
			os.close();
		}
	}
	
	static class MyHandler2 implements HttpHandler {

		public void handle(HttpExchange t) throws IOException {
			String response = "";
			t.sendResponseHeaders(200, response.length());
			OutputStream os = t.getResponseBody();
			os.write(response.getBytes());
			os.close();
		}
	}
	static class MyHandler3 implements HttpHandler {

		public void handle(HttpExchange t) throws IOException {
//			t.getResponseHeaders().add("Content-Type", "application/atom+xml");
			t.sendResponseHeaders(404, 0);
			OutputStream os = t.getResponseBody();
//			os.write(response.getBytes());
			os.flush();
			os.close();
		}
	}

}
